package com.chenchao.collection.stream;

import java.util.ArrayList;
import java.util.stream.Stream;

/**
 * @encoding : UTF-8
 * @Time : 2020/12/4 19:49
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.collection.stream.Test02.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * stream的中间操作
 */
public class Test02 {
    public static void main(String[] args) {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("pear");
        strings.add("apple");
        strings.add("orange");
        strings.add("an apple");
        strings.add("a big cat");
        Stream<String> stream = strings.stream();
        /*流只能使用一次，foreach()之后就死掉了*/
        //test01(stream);
        //test02(stream);
        test03(stream);
    }

    /**
     * 打印a开头的字符串
     * @param stream String流
     */
    public static void test01(Stream<String> stream){
        /*filter(predicate接口)通过返回的boolean进行判断*/
        stream.filter(s -> s.startsWith("a")).
                forEach(System.out::println);
    }

    /**
     * 打印长度大于4的字符串
     * @param stream String流
     */
    public static void test02(Stream<String> stream){
        stream.filter(s->s.length()>4).
                forEach(System.out::println);
    }

    /**
     * 打印长度大于4并且a开头的字符串
     * @param stream String流
     */
    public static void test03(Stream<String> stream){
        stream.filter(s->s.startsWith("a")).
                filter(s->s.length()>4).forEach(System.out::println);
    }
}
